
package com.blackline.auto.User;

import static org.junit.Assert.fail;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.interactions.Actions;

import com.blackline.declareUI.UI;
import com.blackline.parameters.Parameters;
import com.blackline.parameters.TestData;
import com.blackline.auto.common.*;

/**
 * @author XBO
 * @version Feb, 2013
 * <p>The smoke test to Others module.
 */
public class User_smoketest {
	
	private WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();
	private static Logger logger=Logger.getLogger(User_smoketest.class);
	UI UI=new UI();
	Parameters parameters;
	@Before
	public void setUp() throws Exception {
		FirefoxProfile profile=new FirefoxProfile();
		profile.setEnableNativeEvents(true);
		parameters=new Parameters("parameters.xml");

		driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		UI.declareAllUI(driver);
	}
	
	@Test
	public void Usersmoketest() throws Exception {
		TestData testdata=new TestData("TestData/User_smoketest.xml");
		Actions action = new Actions(driver);

		SimpleDateFormat usertail =new SimpleDateFormat("yyyyMMdd-kk-mm-ss");
		Date date = new Date();
		
		String username = "blatuotest"+usertail.format(date);
		//String username = testdata.getInput("ID");
		logger.info("username is: " + username);

		String hotmailInBoxUrl = UI.LiveSite.hotmailInBoxUrl;
		String liveSite = UI.LiveSite.liveSiteUrl;
		
	//Clean the live mail first. Multiple Blackline Password Reset mails lead to error of element recoganization.
		//Login Live mailbox
		//Open Windows Live web site
		logger.info("Going to open: " + liveSite);
		driver.get(liveSite);
		UI.LiveSite.User_textField.sendKeys(testdata.getInput("emailaddress"));
		UI.LiveSite.Password_textField.sendKeys(testdata.getInput("emailpassword"));
		UI.LiveSite.SignIn_button.click();
				
		//Remove all mails in Live mailbox
		driver.get(hotmailInBoxUrl);
		Thread.sleep(15000); //Wait to load all page contents.
		
		//If no mail in Inbox, do nothing; if have emails, remove them.
		if (!UI.LiveSite.No_Message_text.isDisplayed())
		{
			logger.info("Have existing emails. Gonna remove them.");
			UI.LiveSite.checkall_checkbox.click();
			UI.LiveSite.Delete_link.click();
		}

	//Add New User
		UI.LogonPage.Login(driver, parameters.urlDev2, parameters.getLogonID("admin"), parameters.getPassword("admin"));
		UI.NavigationMenu.USERS.click();
		UI.Users.Add_New_User.click();
		verificationErrors.append(UI.page.VerifyPageTitle(driver, "User Details"));
		UI.User_Details.Login_ID.sendKeys(username);
		UI.User_Details.First_Name.sendKeys(testdata.getInput("firstname"));
		UI.User_Details.Last_Name.sendKeys(testdata.getInput("lastname"));
		UI.User_Details.Email_Address.sendKeys(testdata.getInput("emailaddress"));
		
		UI.User_Details.Save_button.click();
		
		
		//Select Entity
		UI.User_Details.Entities_bar.click();
		UI.User_Details.Entity_List.sendKeys("BlackLine International");
		Thread.sleep(2000);
		UI.User_Details.Add_Entity_button.click();
		Thread.sleep(2000);
		UI.User_Details.OK_Confirm.click();
		
		//Assign roles
		UI.User_Details.Authorized_Roles.click();
		UI.User_Details.Sys_Admin_Checkbox.click();
		UI.User_Details.Save_button.click();
		Thread.sleep(2000);
		
		//Check whether the new user exists.
		UI.User_Details.Back_button.click();
		UI.Users.LoginID.sendKeys(username);
		UI.Users.Apply_Link.click();
		
		Thread.sleep(2000);
		if (username.equals(UI.Users.LoginID_verifyfield.getText()))
		{
			logger.info("The user is added.");
		}
		else
		{
			verificationErrors.append("[Fail!] The user is not added.");
			logger.error("The user is not added.");
		}
		
			if (testdata.getInput("activestatus").equals(UI.Users.Active_Status.getText()))
		{
			logger.info("The user is active.");
		}
		else
		{
			verificationErrors.append("[Fail!] The user is inactive.");
			logger.error("The user is inactive.");
		}
	
		
	//Check mail and reset password
		//Open Windows Live web site
		logger.info("Going to open: " + liveSite);
		driver.get(liveSite);
		
		//==================Notice=================//
		//Because we logged in Live maibox at the beginning of this test, the session
		//is still alive. So we needn't loggin again. It will directly enter the site.
		
		//Go to hotmail box
		logger.info("Going to open: " + hotmailInBoxUrl);
		driver.get(hotmailInBoxUrl);
		Thread.sleep(15000); //Wait to load all page contents.
		
		if(!UI.LiveSite.BlackLine_Systems_Password_Reset_link.isDisplayed())
		{
			logger.info("Going to open site again: " + hotmailInBoxUrl);
			driver.get(hotmailInBoxUrl);
			Thread.sleep(15000); //Wait to load all page contents.
		}
		
		//Click the Password Reset mail link
		UI.LiveSite.BlackLine_Systems_Password_Reset_link.click();
		
		//Copy all the page contents and get the URL for resetting password
		action.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();
		action.keyDown(Keys.CONTROL).sendKeys("c").keyUp(Keys.CONTROL).perform();
		CommonMethods cm = new CommonMethods();
		String copiedString = cm.getClipboardStrings();
		String tempFilePath = "testdata\\createuser.txt";
		String pattern = "https://dev.blacklineondemand.com";
		cm.createFile(tempFilePath, copiedString);
		String returnString = cm.getStringLine(tempFilePath,pattern); 
		cm.deleteFile(tempFilePath);
		
		//Remove all mails in Live mailbox
		driver.get(hotmailInBoxUrl);
		Thread.sleep(15000); //Wait to load all page contents.
		UI.LiveSite.checkall_checkbox.click();
		UI.LiveSite.Delete_link.click();
		
		//Open the URL of resetting password
		driver.get(returnString);
		
		//Reset password
		UI.SetPassword.LoginID_textField.sendKeys(username);
		UI.SetPassword.New_Password_textField.sendKeys(testdata.getInput("userPassword"));
		UI.SetPassword.Confirm_Password_textField.sendKeys(testdata.getInput("userPassword"));
		UI.SetPassword.Save_icon.click();
		
		//Login Blackline system with new user
		UI.LogonPage.LoginID.sendKeys(username);//!!!! User this for final script !!!!!!!
		UI.LogonPage.Password.sendKeys(testdata.getInput("userPassword"));
		UI.LogonPage.LoginButton.click();
		UI.SessionMenu.LogOut.click();
	}
	
	
	@After
	public void tearDown() throws Exception {
		CommonMethods.takeScreenShot(this.getClass(), driver);
		driver.close();
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}
	
}
